package project1;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

@Gomoku_Component("悔棋按钮事件监听类")
public class UndoButtonListener implements ActionListener {
    DrawGomoku dg = new DrawGomoku();
    public UndoButtonListener(DrawGomoku dg){
        this.dg = dg;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        //确保有棋子可以悔,这个问题一开始没想到
        if (dg.isBlackChess) {
            //轮到黑棋时，确保白棋有棋子可以悔
            if (!dg.White_chess.isEmpty()) {
                dg.White_chess.remove(dg.White_chess.size() - 1);
                dg.isBlackChess = !dg.isBlackChess;
                dg.repaint();
            }
            else
                System.out.println("白棋无棋子可悔");
        }
        else{
            //轮到白棋时，确保黑棋有棋子可以悔
            if(!dg.Black_chess.isEmpty()){
                dg.Black_chess.remove(dg.Black_chess.size() - 1);
                dg.isBlackChess = !dg.isBlackChess;
                dg.repaint();
            }
            else
                System.out.println("黑棋无棋子可悔");
        }
        System.out.println("悔棋");
    }





}
